package com.syxp.pda.action.base;

import java.util.List;

import javax.annotation.Resource;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.syxp.pda.core.command.QueryFilter;
import com.syxp.pda.core.web.PagingBean;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.base.DefineIDZone;
import com.syxp.pda.entity.base.Station;
import com.syxp.pda.service.base.DefineIDZoneService;

public class ZoneAction extends BaseAction {
	@Resource
	private DefineIDZoneService defineIDZoneService;
	 
	public DefineIDZoneService getDefineIDZoneService() {
		return defineIDZoneService;
	}

	public void setDefineIDZoneService(DefineIDZoneService defineIDZoneService) {
		this.defineIDZoneService = defineIDZoneService;
	}

	private int zoneID;//传入的参数
	
	private int start ;
	
	private int limit ;
	
	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getZoneID() {
		return zoneID;
	}

	public void setZoneID(int zoneID) {
		this.zoneID = zoneID;
	}

	public DefineIDZone getZone() {
		return zone;
	}

	public void setZone(DefineIDZone zone) {
		this.zone = zone;
	}

	private DefineIDZone zone; //注入Station实体类
	
	
	
	public String save(){ //保存
		try{
			System.out.println(zone) ;
			DefineIDZone s= defineIDZoneService.addService(zone) ;
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("zone.zoneID", String.valueOf(zone.getZoneID())) ;
			jobj.addProperty("zone.idCardInfo", zone.getIdCardInfo()) ;
			jobj.addProperty("zone.zoneName", zone.getZoneName()) ;
			jobj.addProperty("zone.zonePlace", zone.getZonePlace()) ;
			jobj.addProperty("success", true) ;
			this.jsonString=gson.toJson(jobj) ;
		}catch(Exception e) {
			e.printStackTrace() ;
		}
		return SUCCESS ;
	}
	
	public String load() {  //加载所有数据
		try{
			List<DefineIDZone> all=null;
		QueryFilter filter = new QueryFilter() ;
		if(limit==0){
			all=defineIDZoneService.getDao().getAll();
		}else{
			filter.setPagingBean(new PagingBean(start,limit)) ;	
			 all = defineIDZoneService.getDao().getAll(filter);
		}
		JsonArray jarray = new JsonArray() ;
		JsonObject result = new JsonObject() ;
		for(DefineIDZone zone : all) {
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("zoneID", zone.getZoneID()) ;
			jobj.addProperty("idCardInfo", zone.getIdCardInfo()) ;
			jobj.addProperty("zoneName", zone.getZoneName()) ;
			jobj.addProperty("zonePlace", String.valueOf(zone.getZonePlace())) ;
			jarray.add(jobj) ;
		}
		if(limit==0){
			this.jsonString = gson.toJson(jarray) ;
		}else{
			result.addProperty("totalProperty",defineIDZoneService.getDao().getTotalProperty(filter)) ;
			result.add("result", jarray) ;
			result.addProperty("start", start);
			result.addProperty("limit", limit) ;
			this.jsonString="{success:true}" ;
			this.jsonString = gson.toJson(result) ;
		}
		
		System.out.println(jsonString) ;
		}catch(Exception e){
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
	public String delete() {  //删除
		System.out.println(zoneID);
		try{
			defineIDZoneService.removeService(zoneID) ;
			this.jsonString="{success:true}" ;
		}catch(Exception e){
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		
		return SUCCESS ;
	}
	public String get()	{  //查看
		try{
			DefineIDZone z = defineIDZoneService.loadService(zoneID) ;
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("zoneID", z.getZoneID()) ;
			jobj.addProperty("idCardInfo", z.getIdCardInfo()) ;
			jobj.addProperty("zoneName", z.getZoneName()) ;
			jobj.addProperty("zonePlace", z.getZonePlace()) ;
			jobj.addProperty("success", true) ;
			this.jsonString = gson.toJson(jobj) ;
			System.out.println(jsonString) ;
		}catch(Exception e) {
			e.printStackTrace();
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
	
	public String update() {
		try{
			defineIDZoneService.updateService(zone) ;
			this.jsonString = "{success:true}" ;
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
}
